import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import os
from tkinter.scrolledtext import ScrolledText
import csv


class UniversalCSVGenerator:
    def __init__(self, root):
        self.root = root
        self.root.title("全语言CSV文件生成器")
        self.root.geometry("800x500")
        self.root.resizable(True, True)
        self.root.configure(bg="#f5f5f5")

        # 创建主框架
        main_frame = ttk.Frame(root, padding=10)
        main_frame.pack(fill=tk.BOTH, expand=True)

        # 标题
        title_label = ttk.Label(
            main_frame,
            text="全语言CSV文件生成器",
            font=("Arial", 16, "bold"),
            foreground="#2c3e50"
        )
        title_label.pack(pady=(0, 10))

        # 语言支持说明
        lang_label = ttk.Label(
            main_frame,
            text="",
            font=("Arial", 10, "italic"),
            foreground="#3498db"
        )
        lang_label.pack(pady=(0, 15))

        # 输入区域框架
        input_frame = ttk.LabelFrame(main_frame, text="输入数据", padding=10)
        input_frame.pack(fill=tk.X, pady=5)

        # 文件名输入
        filename_frame = ttk.Frame(input_frame)
        filename_frame.pack(fill=tk.X, pady=5)

        ttk.Label(filename_frame, text="文件名:").pack(side=tk.LEFT, padx=(0, 5))

        self.filename_entry = ttk.Entry(filename_frame, width=25)
        self.filename_entry.pack(side=tk.LEFT)
        self.filename_entry.insert(0, "我的数据")

        self.filename_preview = ttk.Label(
            filename_frame,
            text="生成的文件名: 我的数据.csv",
            foreground="#3498db",
            font=("Arial", 9)
        )
        self.filename_preview.pack(side=tk.LEFT, padx=(10, 0))

        # 内容输入区域
        content_frame = ttk.Frame(input_frame)
        content_frame.pack(fill=tk.BOTH, pady=5, expand=True)

        ttk.Label(content_frame, text="CSV内容 (每行一条记录):").pack(anchor=tk.W)

        self.content_text = ScrolledText(
            content_frame,
            width=60,
            height=10,
            font=("Arial", 10),
            wrap=tk.NONE
        )
        self.content_text.pack(fill=tk.BOTH, expand=True)

        # 添加示例内容
        example_content = (

        )
        self.content_text.insert(tk.END, example_content)

        # 提示信息
        tip_frame = ttk.Frame(input_frame)
        tip_frame.pack(fill=tk.X, pady=(5, 0))

        tip_label = ttk.Label(
            tip_frame,
            text="提示: 自动支持所有语言，无需特殊处理逗号",
            foreground="#3498db",
            font=("Arial", 9)
        )
        tip_label.pack(anchor=tk.W)

        # 按钮区域
        button_frame = ttk.Frame(main_frame)
        button_frame.pack(fill=tk.X, pady=10)

        # 清除按钮
        self.clear_btn = ttk.Button(
            button_frame,
            text="清除内容",
            command=self.clear_content,
            width=12
        )
        self.clear_btn.pack(side=tk.LEFT, padx=(0, 10))

        # 生成按钮 - 使用黑色文案
        self.generate_btn = ttk.Button(
            button_frame,
            text="生成CSV文件",
            command=self.generate_csv,
            style="Black.TButton",
            width=15
        )
        self.generate_btn.pack(side=tk.RIGHT)

        # 状态栏
        self.status_bar = ttk.Frame(main_frame, height=20)
        self.status_bar.pack(fill=tk.X, pady=(5, 0))

        self.status_label = ttk.Label(
            self.status_bar,
            text="就绪 | 自动支持en, it, de, fr, pt, pl, es, ja, ko, zh-cn, zh-tw",
            anchor=tk.W,
            background="#e0e0e0",
            padding=3,
            font=("Arial", 9)
        )
        self.status_label.pack(fill=tk.X)

        # 绑定事件
        self.filename_entry.bind("<KeyRelease>", self.update_filename_preview)

        # 配置样式
        self.configure_styles()

    def configure_styles(self):
        style = ttk.Style()
        # 生成按钮使用黑色文案
        style.configure("Black.TButton",
                        foreground="black",
                        background="#e0e0e0",
                        font=("Arial", 10, "bold"))
        style.map("Black.TButton",
                  background=[("active", "#d0d0d0"), ("pressed", "#c0c0c0")])

        style.configure("TButton", padding=5)
        style.configure("TLabelFrame", padding=5)

    def update_filename_preview(self, event=None):
        filename = self.filename_entry.get().strip()
        if not filename:
            self.filename_preview.config(text="生成的文件名: 未命名.csv")
        else:
            self.filename_preview.config(text=f"生成的文件名: {filename}.csv")

    def clear_content(self):
        self.content_text.delete("1.0", tk.END)
        self.status_label.config(text="内容已清除")

    def generate_csv(self):
        filename = self.filename_entry.get().strip()
        if not filename:
            filename = "未命名"

        content = self.content_text.get("1.0", tk.END).strip()
        if not content:
            messagebox.showwarning("内容为空", "请输入CSV内容")
            return

        # 打开文件保存对话框
        filepath = filedialog.asksaveasfilename(
            initialfile=f"{filename}.csv",
            defaultextension=".csv",
            filetypes=[("CSV文件", "*.csv"), ("所有文件", "*.*")]
        )

        if not filepath:
            return  # 用户取消了保存

        try:
            # 处理内容 - 每行作为单列
            lines = content.split('\n')

            with open(filepath, 'w', encoding='utf-8', newline='') as f:
                writer = csv.writer(f)
                for line in lines:
                    # 直接写入单列数据
                    writer.writerow([line])

            self.status_label.config(text=f"文件已成功保存到: {filepath}")
            messagebox.showinfo("成功", f"CSV文件已成功保存到:\n{filepath}")

        except Exception as e:
            self.status_label.config(text=f"保存文件时出错: {str(e)}")
            messagebox.showerror("错误", f"保存文件时出错:\n{str(e)}")


if __name__ == "__main__":
    root = tk.Tk()
    app = UniversalCSVGenerator(root)
    root.mainloop()